﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace WebApplication1
{
    public partial class _Default : Page
    {
        private static DataTable Data = new DataTable();
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                FillData();
            }
        }

        private void FillData()
        {
            var sql = "select * from Info";
            var dt = DbHelper.GetDataTable(sql);

            GridView1.DataSource = dt;
            GridView1.DataBind();
        }

        //
        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GridView1.EditIndex = e.NewEditIndex;
            FillData();
        }
        //
        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            var Id = GetValue(e.RowIndex, 0);
            var Name = GetValue(e.RowIndex, 1);
            var password = GetValue(e.RowIndex, 2);
            var Remarks = GetValue(e.RowIndex, 3);

            var Get = int.TryParse(Id, out int id);

            if(Get)
            {
                //更新
                var sql = string.Format("update Info set Name = '{0}',password ='{1}',Remarks='{2}' where Id = '{3}'", Name, password, Remarks, Id);
                DbHelper.AddOrData(sql);
            }
            else
            {
                //添加
                var sql = string.Format("insert into Info (Name,password,Remarks)values('{0}','{1}','{2}')", Name, password, Remarks);
                DbHelper.AddOrData(sql);
            }
            GridView1.EditIndex = -1;
            FillData();
        }

        private string GetValue(int rowIndex, int v)
        {
            var bet = GridView1.Rows[rowIndex].Cells[v];

            string values = string.Empty;

            if (bet.Controls .Count >0)
            {
                var h = bet.Controls[0];
                var row = (TextBox)h;
                values = row.Text;
            }
            else
            {
                values = bet.Text;
            }
            return values;
        }

        //
        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            var Id = GetValue(e.RowIndex, 0);

            var sql = string.Format("delete from Info where Id ='{0}'", Id);
            DbHelper.AddOrData(sql);

            GridView1.EditIndex = -1;
            FillData();
        }
        //
        protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            GridView1.EditIndex = -1;
            FillData();
        }

        protected void Add_Click(object sender, EventArgs e)
        {
            var sql = "select * from Info";
            Data = DbHelper.GetDataTable(sql);

            var dt = Data.Copy();
            Data = dt;
            dt.Rows.Add(dt.NewRow());

            GridView1.DataSource = Data;
            GridView1.EditIndex = dt.Rows.Count - 1;

            GridView1.DataSource = Data;
            GridView1.DataBind();
        }
    }
}